/* 初始化 */
$(function(){
		var state = $("#state").val();
		var url = "accountsurroundjson?state=" + state;
		$('#dd').datagrid({
    		url:url,
    		collapsible:true,
    		fit:true,
			fitColumns:true,
			toolbar:'#footer',
			pagination:true,
			pageSize:20,
			pageList:[20,50,100,200],
			remoteSort:false,
			onLoadSuccess:function(data){
		        if (data.total == 0) { 
				$(this).datagrid('appendRow', { cVoucher: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'cVoucher', colspan: 4 }) 
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); 
				 $(this).datagrid('hideColumn', 'ck');
				} 
				else {
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
					$(this).datagrid('showColumn', 'ck');}
		    },
   			columns:[[
   			 	{field:'ck',checkbox:true},
   			 	{field:'iID',hidden:'true'},
				{field:'cVoucher',title:'收据号',align:'center',width:'25%',
				 formatter:function(value,row,index){
					if(row.iID == undefined)return row.cVoucher;
					return'<a href="javascript:;">'+row.cVoucher+'</a>'
				}},
				{field:'fReceipt',title:'实收款',align:'center',width:'25%'},
				{field:'fRefund',title:'实退款',align:'center',width:'23%'},
				{field:'iAccountState',title:'审核状态',align:'center',width:'25%',
					formatter:function(value,row,index){
						if(row.iAccountState == undefined)return '异常';
						if(row.iAccountState == 0)return '未审核';
						if(row.iAccountState == 1)return '通过';
						if(row.iAccountState == 2)return '未通过';
					}
				
				}
    		]]
		});		
});

/*搜索*/
function search(){
		var accountState = $("#accountState").val();
		$("#dd").datagrid("load",{
			accountState:accountState
		})
	}
	
/*初审*/
function accountFirst(){
	var rows = $('#dd').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 }else{
			var checklist = new Array();
			var checkName = [];
			checkName.push('选择了以下订单');
			for(var i=0; i<rows.length; i++){
				checklist.push(rows[i].iID);
				checkName.push('<span>'+rows[i].cVoucher +'</span>');
			}
			layer.confirm('请选择', {
			  btn: ['通过','驳回'] 
			}, function(){
			  accountPost(checklist,3);
			}, function(){
			  accountPost(checklist,2);
			});
	}
}

/*复审*/
function accountSecond(){
	var rows = $('#dd').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 }else{
			var checklist = new Array();
			var checkName = [];
			checkName.push('选择了以下订单');
			for(var i=0; i<rows.length; i++){
				checklist.push(rows[i].iID);
				checkName.push('<span>'+rows[i].cVoucher +'</span>');
			}
			layer.confirm('请选择', {
			  btn: ['通过','驳回'] 
			}, function(){
			  accountPost(checklist,1);
			}, function(){
			  accountPost(checklist,2);
			});	
	}
}

/* 审核请求 */
function accountPost(checklist,accountState){
	$.ajax({
		url:"/mmy/accountsurroundcheck",
		type:"post",
		data:{
			checkList:checklist.toString(),
			accountState:accountState
		},
		dataType:"text",
		success:function () {
			layer.closeAll();
			$("#dd").datagrid("reload");
			$.messager.show({title:'消息',msg:'成功',timeout:3000,});
		},
		error:function () {
			layer.closeAll();
			$.messager.show({title:'消息',msg:'失败',timeout:3000,});
		}
	});	
}

